package com.stgl.service;

import com.stgl.dao.StudentDao;
import com.stgl.dao.WechartDao;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class WechartService {
    @Autowired
    private WechartDao wechartDao;

    @Autowired
    private StudentDao studentDao;

    public List<Map<String,Object>>getuserBindStu(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=wechartDao.getuserBindStu(gmap);
        return list1;
    }


    @Cacheable(cacheNames = {"school"})
    public List<Map<String,Object>>getAllSchool()
    {
        List<Map<String,Object>>list1=wechartDao.getAllSchool();
        return list1;
    }


    @Cacheable(cacheNames = {"area"})
    public List<Map<String,Object>>getBzqArea()
    {
       /* List<Map<String,Object>>list1=wechartDao.getBzqTown();
        for(int i=0;i<list1.size();i++){
            Map<String,Object>maps=list1.get(i);
            String tnid=maps.get("TNID").toString();
            List<Map<String,Object>>list2=wechartDao.getBzqVillageTown(tnid);
            maps.put("village",list2);
        }*/
       //获取播州区南白镇下的所有社区
        List<Map<String,Object>>list1=wechartDao.getBzqLbz();
        return list1;
    }

    public List<Map<String,Object>>getCampusStudentBysfzh(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=wechartDao.getCampusStudentBysfzh(gmap);
        return list1;
    }


    public int insertStudent(Map<String,Object>gmap)
    {
        return wechartDao.insertStudent(gmap);
    }



    public List<Map<String,Object>>studentList(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=wechartDao.studentList(gmap);
    /*    for(int i=0;i<list1.size();i++){
            Map<String,Object>smap=list1.get(i);
            String hjszdxq="";
            if(smap.get("HJSZD")!=null){
                String hjszd=smap.get("HJSZD").toString();
                String []hjarr=hjszd.split(",");
                if(hjarr.length>0){
                    if(hjarr[0]!=null){
                        //根据地址id查询名称
                        String tnid=hjarr[0];
                        List<Map<String,Object>>town=wechartDao.getTownByHjszd(tnid);
                        if(town.size()>0){
                            String zhen=town.get(0).get("TNNAME").toString();
                            hjszdxq=zhen;
                        }
                    }
                }

                if(hjarr.length>1){
                    if(hjarr[1]!=null && hjarr[1]!=""){
                        //根据地址id查询名称
                        List<Map<String,Object>>village=wechartDao.getCunByHjszd(hjarr[1]);
                        if(village.size()>0){
                            String cun=village.get(0).get("VANAME").toString();
                            hjszdxq=hjszdxq+","+cun;
                        }
                    }
                }

            }
            smap.put("hjszdxq",hjszdxq);
        }*/
        return list1;
    }
    public int studentCount(Map<String,Object>gmap)
    {
        return wechartDao.studentCount(gmap);
    }


    public int cancelBM(String sid)
    {
        return wechartDao.cancelBM(sid);
    }




    public Map<String,Object> studentDetail(String sid)
    {
        List<Map<String,Object>>list1=wechartDao.studentDetail(sid);
        if(list1.size()>0){
            Map<String,Object>smap=list1.get(0);
            //查询相关图片
            Map<String,Object>simage=new HashMap<>();
            simage.put("tablename","student");
            simage.put("zjid",sid);
            simage.put("zdmc","hjszd");
            List<Map<String,Object>>list2=wechartDao.getListImageUrl(simage);
            smap.put("imgurls",list2);
            return smap;
        }
        return new HashMap<>();
    }




    public int updateStudent(Map<String,Object>gmap)
    {
        return wechartDao.updateStudent(gmap);
    }


    public Map<String,Object>getSystemInfo()
    {
        List<Map<String,Object>>list1=new ArrayList<>();
        list1=wechartDao.getSystemInfo();
        if(list1.size()>0){
            return list1.get(0);
        }
        return new HashMap<>();
    }

    public String getAgeByBirth(Map<String,Object>gmap)
    {
        String age=wechartDao.getAgeByBirth(gmap);
        return age;
    }



    public List<Map<String,Object>>getBmcgListByCsid(String csid)
    {
        List<Map<String,Object>>list1=new ArrayList<>();
        Calendar cale = null;
        cale = Calendar.getInstance();
        int nowyear = cale.get(Calendar.YEAR);
        Map<String,Object>gmap=new HashMap<String,Object>();
        gmap.put("csid",csid);
        gmap.put("nowyear",nowyear);
        list1=wechartDao.getBmcgListByCsid(gmap);
        return list1;
    }


    //查询下一个应该获取的报名号
    public synchronized String getCampusStudentNextBmh(Map<String,Object>gmap)
    {
        String bmh=gmap.get("csnum").toString();
        String nj=gmap.get("nj").toString();
        int maxbmh=wechartDao.getCampusStudentMaxBmh(gmap);
        if(maxbmh==0){
            bmh=bmh+nj+"0001";
        }else{
            //查询是否有遗漏的编号
            String csbh=bmh+nj+"0001";
            gmap.put("csbh",csbh);
            List<Map<String,Object>>list=wechartDao.getCampusStudentYlBmh(gmap);
            if(list.size()>0){
                //有遗漏编码 用原编码
                bmh=list.get(0).get("FINT").toString();
            }else{
                //没有遗漏编码 则最大编码加1
                bmh=(maxbmh+1)+"";
            }
        }
        return bmh;
    }


    //报名同步
    public synchronized Map BmxtInsert(List<String>listr,Map<String, Object>gmap)
    {
        String bmh=gmap.get("csnum").toString();
        String nj=gmap.get("nj").toString();
        int maxbmh=wechartDao.getCampusStudentMaxBmh(gmap);
        if(maxbmh==0){
            bmh=bmh+nj+"0001";
        }else{
            //查询是否有遗漏的编号
            String csbh=bmh+nj+"0001";
            gmap.put("csbh",csbh);
            List<Map<String,Object>>list=wechartDao.getCampusStudentYlBmh(gmap);
            if(list.size()>0){
                //有遗漏编码 用原编码
                bmh=list.get(0).get("FINT").toString();
            }else{
                //没有遗漏编码 则最大编码加1
                bmh=(maxbmh+1)+"";
            }
        }
        gmap.put("bmh",bmh);

        //定义返回结果map
        Map<String, Object>rmap=new HashMap<String, Object>();
        //先上传图片
        for(int k=0;k<listr.size();k++){
            Map<String, Object>zmap=new HashMap<String, Object>();
            zmap.put("imageurl",listr.get(k).toString());
            zmap.put("tablename","student");
            zmap.put("zjid",gmap.get("sid"));
            zmap.put("zdmc","hjszd");
            int m=studentDao.Insert_stufile(zmap);
            if(m<0){
                rmap.put("result",false);
                rmap.put("msg","图片上传失败!");
                return rmap;
            }
        }
        int i=wechartDao.insertStudent(gmap);
        if(i>0){
            rmap.put("result",true);
            rmap.put("msg","报名成功！");
            return rmap;
        }
        rmap.put("result",false);
        rmap.put("msg","报名失败,请联系学校管理员！");
        return rmap;
    }

    //因为数据同步问题 可能会有重复  重复的往后移号
    //  @Scheduled(fixedRate=1800000)
  /*  public void getCfsj()
    {
        //查询重复的编号
        List<Map<String,Object>>list1=new ArrayList<>();
        list1=wechartDao.getCfsj();
        if(list1.size()>0){
            String nbmh="";
            for(int i=0;i<list1.size();i++){
                String bmh=list1.get(i).get("BMH").toString();
                String sid=list1.get(i).get("SID").toString();
                if(bmh.equals(nbmh)){
                    String csnum=bmh.substring(0,2);
                    String nj=bmh.substring(2,1);
                    Map<String,Object>maps=new HashMap<>();
                    maps.put("csnum",csnum);
                    maps.put("nj",nj);
                    maps.put("sid",sid);
                    String nextbmh=getCampusStudentNextBmh(maps);
                    maps.put("bmh",nextbmh);
                    wechartDao.updateStudentBmh(maps);
                }else{
                    nbmh=list1.get(i).get("BMH").toString();
                }
            }
        }
    }
*/



    public int getNjByCsrq(String csrq)
    {
        int nj=wechartDao.getNjByCsrq(csrq);
        return nj;
    }

    public boolean insertTsm(Map<String,Object>gmap)
    {
        int u=wechartDao.insertTsm(gmap);
        if(u>0){
            return true;
        }
        return false;
    }

    //根据儿童生日和姓名判断该儿童是否重复报名
    public List<Map<String,Object>>getCampusStudentByBirthAndName(Map<String,Object>gmap)
    {
        List<Map<String,Object>>list1=wechartDao.getCampusStudentByBirthAndName(gmap);
        return list1;
    }



}
